package com.why.dao.mybatis;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Component;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.why.entity.Index;
import com.why.entity.IndexData;

@Component
public class IndexDao extends SqlMapClientDaoSupport {

	@SuppressWarnings("unchecked")
	public List<Index> getaAll() {
		// TODO Auto-generated method stub
		List<Index> ps = getSqlMapClientTemplate().queryForList("IndexInfo.findAll");
		return ps;
	}

	@SuppressWarnings("unchecked")
	public List<Index> getIndexByCode(String index_code) {
		// TODO Auto-generated method stub
		List<Index> ps = getSqlMapClientTemplate().queryForList("IndexInfo.findByCode",index_code);
		return ps;
	}

	public String getIndexPathById(String id) {
		// TODO Auto-generated method stub
		String path = (String) getSqlMapClientTemplate().queryForObject("IndexInfo.getPath",id);
		return path;
	}

	public List<Index> getSubIndexs(String path) {
		// TODO Auto-generated method stub
		List<Index> indexs = getSqlMapClientTemplate().queryForList("IndexInfo.getSubIndexs",path+"%");
		return indexs;
	}
	
	
	public IndexData getIndexData(String year, String org_id,String index_id) {
		// TODO Auto-generated method stub
		Map<String,String> map = new HashMap<String,String>();
		map.put("year", year);
		map.put("org_id", org_id);
		map.put("index_id", index_id);
		IndexData orgs = (IndexData) getSqlMapClientTemplate().queryForObject("IndexInfo.getIndexData",map);
		return orgs;
	}
	
	public void insertIndexData(IndexData index_data){
		getSqlMapClientTemplate().insert("IndexInfo.insertIndexData", index_data);
	}
	
	public void insertIndexDataBatch(final List<IndexData> lists) throws DataAccessException{
		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback(){
			//@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				// TODO Auto-generated method stub
				executor.startBatch(); 
				int batch = 0;
				for(IndexData index_data:lists){
					executor.insert("IndexInfo.insertIndexData", index_data); 
					batch++;
					if(batch==500){ 
					    executor.executeBatch(); 
					    batch = 0; 
					}
				}
				executor.executeBatch();
				return null;
			}
	    }); 
	}

	public void updateIndexData(IndexData index_data){
		getSqlMapClientTemplate().insert("IndexInfo.updateIndexData", index_data);
	}
	
	public void updateIndexDataBatch(final List<IndexData> lists) throws DataAccessException{
		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback(){
			//@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				// TODO Auto-generated method stub
				executor.startBatch(); 
				int batch = 0;
				for(IndexData index_data:lists){
					executor.insert("IndexInfo.updateIndexData", index_data); 
					batch++;
					if(batch==500){ 
					    executor.executeBatch(); 
					    batch = 0; 
					}
				}
				executor.executeBatch();
				return null;
			}
	    }); 
	}

	public void updateIndexInfo(Index index) {
		// TODO Auto-generated method stub
		getSqlMapClientTemplate().update("IndexInfo.updateIndexInfo", index);
	}

	public void insertIndexInfo(Index index) {
		// TODO Auto-generated method stub
		getSqlMapClientTemplate().insert("IndexInfo.insertIndexInfo", index);
	}
	
	public void deleteIndexInfo(String id) {
		// TODO Auto-generated method stub
		int r = getSqlMapClientTemplate().delete("IndexInfo.deleteIndexInfo", id);
		System.out.println("delete:"+r);
	}
}
